﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
//using System.Windows.Shapes;
//
// Mp3Cleaner, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
namespace Mp3Cleaner
{
    public partial class MainWindow : Window
    {
        private TextBlock currentTextBlock;
        private string currentColumnName;
        private void dataGrid1_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //
            if (0 == LD.Count)
            {
                e.Handled = true;
                return;
            }
            //
            DependencyObject dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is DataGridCell) && !(dep is System.Windows.Controls.Primitives.DataGridColumnHeader))
            {
                if (!(dep is DependencyObject)) { dep = null; break; }
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null) return;
            //
            if (dep is System.Windows.Controls.Primitives.DataGridColumnHeader)
            {
                System.Windows.Controls.Primitives.DataGridColumnHeader columnHeader = dep as System.Windows.Controls.Primitives.DataGridColumnHeader;
                currentTextBlock = null;
                currentColumnName = columnHeader.Column.SortMemberPath;
                switch (columnHeader.Column.SortMemberPath)
                {
                    case "tagArtist":
                        selectContextMenuPart(ContextMenuKind.TextOperations);
                        e.Handled = true;
                        return;
                    //
                    case "FileName":
                    case "tagTitle":
                    case "tagAlbum":
                    case "tagGenre":
                        selectContextMenuPart(ContextMenuKind.TextOperations);
                        e.Handled = true;
                        return;
                    //
                    case "tagTrack"://***
                        selectContextMenuPart(ContextMenuKind.TrackNumbersOperations);
                        e.Handled = true;
                        return;
                    //
                    default:
                        selectContextMenuPart(ContextMenuKind.Disabled);
                        e.Handled = true;
                        return;
                }
                e.Handled = false;
                return;
            }
            //
            if (dep is DataGridCell)
            {
                DataGridCell cell = dep as DataGridCell;
                currentTextBlock = cell.Content as TextBlock;
                currentColumnName = cell.Column.SortMemberPath;
                switch (cell.Column.SortMemberPath)
                {
                    case "tagArtist":
                        selectContextMenuPart(ContextMenuKind.TextOperations);
                        e.Handled = true;
                        return;
                    //
                    case "FileName":
                    case "tagTitle":
                    case "tagAlbum":
                    case "tagGenre":
                        selectContextMenuPart(ContextMenuKind.TextOperations);
                        e.Handled = true;
                        return;
                    //
                    case "tagTrack"://***
                        //
                        TextBlock tb = cell.Content as TextBlock;
                        if (null != tb)
                        {
                            if (Utils.isTrackNumberEmpty(((TextBlock)cell.Content).Text))
                            {
                                // działa tylko w pustym polu
                                selectContextMenuPart(ContextMenuKind.NextTrackNumber);
                                e.Handled = true;
                                return;
                            }
                        }
                        break;
                    //
                    default:
                        selectContextMenuPart(ContextMenuKind.Disabled);
                        e.Handled = true;
                        return;
                }
                e.Handled = false;
                return;
            }
            e.Handled = false;
            return;
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------
        private void sentenceCaseToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.SentenceCase);
        }

        private void lowercaseToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.lowercase);
        }

        private void uPPERCASEToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.UPPERCASE);
        }

        private void capitalizeEachWordToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.CapitalizeEachWord);
        }

        private void tOGGLECASEToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.tOGGLEcASE);
        }

        private void SwaplastFirstToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            changeCase(currentTextBlock, currentColumnName, CaseUtils.CASE.SwapLastFirst);
        }

        private void nextNumberToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (null != currentTextBlock)
            {
                string trackNo = currentTextBlock.Text;
                if (Utils.isTrackNumberEmpty(trackNo))
                {
                    int n = getMaxTrackNumber() + 1;
                    currentTextBlock.Text = GetLegalTrackNumber(n.ToString());
                    //((CData)currentTextBlock.DataContext).tagTrack_Changed = true;
                    //((CData)currentTextBlock.DataContext).Marker = true;
                }
            }
        }

        private void clearAllNumbersToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            for (int k = 0; k < LD.Count; k++)
            {
                LD[k].tagTrack = GetLegalTrackNumber("");
                //LD[k].tagTrack_Changed = true;
                //LD[k].Marker = true;
            }
        }

        private void renumberToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            RenumberTracks();
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------
        private enum ContextMenuKind { TextOperations, NextTrackNumber, TrackNumbersOperations, Disabled };
        //---------------------------------------------------------------------------------------------------------------------------------
        private void selectContextMenuPart(ContextMenuKind cm)
        {
            switch (cm)
            {
                case ContextMenuKind.TextOperations:
                    sentenceCaseToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    lowercaseToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    uPPERCASEToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    capitalizeEachWordToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    tOGGLECASEToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    SwaplastFirstToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    //
                    nextNumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    clearAllNumbersToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    renumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    menuNone.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    break;
                case ContextMenuKind.NextTrackNumber:
                    //
                    sentenceCaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    lowercaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    uPPERCASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    capitalizeEachWordToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    tOGGLECASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    SwaplastFirstToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    nextNumberToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    //
                    clearAllNumbersToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    renumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    menuNone.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    break;
                case ContextMenuKind.TrackNumbersOperations:
                    //
                    sentenceCaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    lowercaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    uPPERCASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    capitalizeEachWordToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    tOGGLECASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    SwaplastFirstToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    nextNumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    clearAllNumbersToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    renumberToolStripMenuItem.Visibility = System.Windows.Visibility.Visible;
                    menuNone.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    break;
                case ContextMenuKind.Disabled:
                    menuNone.Visibility = System.Windows.Visibility.Visible;
                    sentenceCaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    lowercaseToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    uPPERCASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    capitalizeEachWordToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    tOGGLECASEToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    SwaplastFirstToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    nextNumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    clearAllNumbersToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    renumberToolStripMenuItem.Visibility = System.Windows.Visibility.Collapsed;
                    //
                    break;
            }
            //---------------------------------------------------------------------------------------------------------------------------------
        }
    }//class
}//namespace
